package com.charger.cooperation.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.charger.chargingStation.domain.Chargingstation;
import com.charger.chargingStation.service.IChargingstationService;
import com.charger.cooperation.domain.InvestorsProjects;
import com.charger.cooperation.domain.Projects;
import com.charger.cooperation.service.IInvestorsProjectsService;
import com.charger.cooperation.service.IProjectsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.charger.common.annotation.Log;
import com.charger.common.core.controller.BaseController;
import com.charger.common.core.domain.AjaxResult;
import com.charger.common.enums.BusinessType;
import com.charger.cooperation.domain.Investors;
import com.charger.cooperation.service.IInvestorsService;
import com.charger.common.utils.poi.ExcelUtil;
import com.charger.common.core.page.TableDataInfo;

/**
 * 投资商管理Controller
 * 
 * @author ruoyi
 * @date 2024-11-22
 */
@RestController
@RequestMapping("/cooperation/investors")
public class InvestorsController extends BaseController
{
    @Autowired
    private IInvestorsService investorsService;
    @Autowired
    private IProjectsService projectsService;
    @Autowired
    private IChargingstationService chargingstationService;
    @Autowired
    private IInvestorsProjectsService investorsProjectsService;


    /**
     * 查询投资商管理列表
     */
    @PreAuthorize("@ss.hasPermi('cooperation:investors:list')")
    @GetMapping("/list")
    public TableDataInfo list(Investors investors)
    {
        startPage();
        List<Investors> list = investorsService.selectInvestorsList(investors);
        for (Investors investor : list) {
            // 根据投资者的ID查询关联的项目列表（注意方法名的修改）
            List<Projects> projectsList =  projectsService.selectById(investor.getInvestorId());
            // 将查询到的项目列表设置到投资者对象中
            investor.setProjectsList(projectsList);
        }

        for (Investors investor : list) {
            // 根据投资者的ID查询关联的项目列表（注意方法名的修改）
            List<Chargingstation> chargingstationList =  chargingstationService.selectById(investor.getInvestorId());
            // 将查询到的项目列表设置到投资者对象中
            investor.setChargingstations(chargingstationList);
        }
        return getDataTable(list);
    }


    /**
     * 导出投资商管理列表
     */
    @PreAuthorize("@ss.hasPermi('cooperation:investors:export')")
    @Log(title = "投资商管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Investors investors)
    {
        List<Investors> list = investorsService.selectInvestorsList(investors);
        ExcelUtil<Investors> util = new ExcelUtil<Investors>(Investors.class);
        util.exportExcel(response, list, "投资商管理数据");
    }

    /**
     * 获取投资商管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('cooperation:investors:query')")
    @GetMapping(value = "/{investorId}")
    public AjaxResult getInfo(@PathVariable("investorId") Long investorId)
    {
        return success(investorsService.selectInvestorsByInvestorId(investorId));
    }

    /**
     * 新增投资商管理
     */
    @PreAuthorize("@ss.hasPermi('cooperation:investors:add')")
    @Log(title = "投资商管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Investors investors)
    {
        toAjax(investorsService.insertInvestors(investors));
        Long investorId = investors.getInvestorId();
        if(investors.getProjectsId() != null){
            for(Long projectId : investors.getProjectId()){
                InvestorsProjects investorsProjects = new InvestorsProjects();
                investorsProjects.setInvestors(investorId);
                investorsProjects.setProjects(projectId);
                investorsProjectsService.insertInvestorsProjects(investorsProjects);
            }
        }
        return success("成功");
    }

    /**
     * 修改投资商管理
     */
    @PreAuthorize("@ss.hasPermi('cooperation:investors:edit')")
    @Log(title = "投资商管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Investors investors)
    {
         toAjax(investorsService.updateInvestors(investors));
        Long id = investors.getInvestorId();
        QueryWrapper<InvestorsProjects> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("investors",id);
//        investorsProjectsService.remove(queryWrapper);
        for(Long projectId : investors.getProjectId()){
            InvestorsProjects investorsProjects = new InvestorsProjects();
            investorsProjects.setInvestors(id);
            investorsProjects.setProjects(projectId);
            investorsProjectsService.insertInvestorsProjects(investorsProjects);
        }
        return success("成功修改");
    }

    /**
     * 删除投资商管理
     */
    @PreAuthorize("@ss.hasPermi('cooperation:investors:remove')")
    @Log(title = "投资商管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{investorIds}")
    public AjaxResult remove(@PathVariable Long[] investorIds)
    {
        return toAjax(investorsService.deleteInvestorsByInvestorIds(investorIds));
    }
}
